<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class makul extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_makul');
        $this->load->model('m_tahunajar');
        // load library
        $this->load->library('tnotification');
        $this->load->library('pagination');
    }

    // halaman list data
    public function index() {
        // set page rules
        $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "pengaturan/makul/index.html");
        // tahun ajar
        $rs_tahunajar = $this->m_tahunajar->get_detail_data_tahunajar();
        $this->smarty->assign('rs_tahunajar', $rs_tahunajar);
        // get data
        $rs_id = $this->m_makul->get_data_group_makul();
        $this->smarty->assign('rs_id', $rs_id);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses penambahan grup
    public function add_group_process() {
        // set page rules
        $this->_set_page_rule("C");
        // tahun ajaran aktif
        $rs_tahunajar = $this->m_tahunajar->get_detail_data_tahunajar();
        if (empty($rs_tahunajar)) {
            // default error
            $this->tnotification->sent_notification("error", "Tahun Ajaran belum di setting !");
            // default redirect
            redirect('pengaturan/makul');
        }
        // cek 
        $cek_group = $this->m_makul->is_setting_makul_exist(array($rs_tahunajar['tahun'], $rs_tahunajar['semester']));
        //--
        if ($cek_group == 0) {
            //params
            $tahun = $rs_tahunajar['tahun'];
            $semester = $rs_tahunajar['semester'];
            //--
            $params = array($semester, $tahun, $this->com_user['user_id']);
            // insert
            if ($this->m_makul->insert_setting_makul($params)) {
                // sukses
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Pengaturan baru telah dibuat!");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan !");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Pengaturan untuk semester ini sudah ada !");
        }
        // default redirect
        redirect('pengaturan/makul');
    }

    // halaman edit group
    public function edit_group($set_makul_kd = "") {
        // set page rules
        $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "pengaturan/makul/detail.html");
        // detail grup
        $rs_group = $this->m_makul->get_detail_setting_makul($set_makul_kd);
        $this->smarty->assign('rs_group', $rs_group);
        // data session
        $search = $this->tsession->userdata('detail_set_makul');
        $this->smarty->assign('search', $search);
        // params
        $makul_nm = !empty($search['makul_nm']) ? "%" . $search['makul_nm'] . "%" : "%";
        //--
        $params = array($set_makul_kd, $makul_nm);
        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("pengaturan/makul/edit_group/" . $set_makul_kd . "/index/");
        $config['total_rows'] = $this->m_makul->get_total_data_set_makul_detail($params);
        $config['uri_segment'] = 6;
        $config['per_page'] = 20;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(6, 0) + 1;
        $end = $this->uri->segment(6, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */
        // get data
        $params = array($set_makul_kd, $makul_nm, ($start - 1), $config['per_page']);
        $result = $this->m_makul->get_data_set_makul_detail($params);
        $this->smarty->assign('rs_id', $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses search detail setting makul 
    public function search_process() {
        // set page rules
        $this->_set_page_rule("R");
        //--
        if ($this->input->post('save') == 'Cari') {
            $params = array(
                "makul_nm" => $this->input->post('makul_nm')
            );
            // set session
            $this->tsession->set_userdata('detail_set_makul', $params);
        } else {
            // set session
            $this->tsession->set_userdata('detail_set_makul');
        }
        // rediredt
        redirect('pengaturan/makul/edit_group/' . $this->input->post('set_makul_kd'));
    }

    // halaman tambah makul
    public function add_makul($set_makul_kd = "") {
        // set page rules
        $this->_set_page_rule("C");
        // set template content
        $this->smarty->assign("template_content", "pengaturan/makul/add_makul.html");
        // load javascript
        $this->smarty->load_javascript("resource/js/jquery/select2-3.4.5/select2.min.js");
        // load css 
        $this->smarty->load_style("select2/select2.css");
        // detail grup
        $rs_group = $this->m_makul->get_detail_setting_makul($set_makul_kd);
        $this->smarty->assign('rs_group', $rs_group);
        // data makul
        $this->smarty->assign('rs_makul', $this->m_makul->get_all_data_makul());
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses tambah makul
    public function add_makul_process() {
        // set page rules
        $this->_set_page_rule("C");
        // cek input
        $this->tnotification->set_rules('makul_kd', 'Mata Kuliah', 'required');
        $this->tnotification->set_rules('set_jur_1', 'D3TI');
        $this->tnotification->set_rules('set_jur_2', 'D3MI');
        $this->tnotification->set_rules('set_jur_3', 'S1TI');
        $this->tnotification->set_rules('set_jur_4', 'S1SI');
        $this->tnotification->set_rules('set_makul_kd', 'Kode Grup', 'required');
        // cek makul
        $makul_kd = $this->input->post('makul_kd');
        $set_makul_kd = $this->input->post('set_makul_kd');
        $cek = $this->m_makul->is_detail_setting_makul_exist(array($set_makul_kd, $makul_kd));
        if ($cek > 0) {
            // set error
            $this->tnotification->set_error_message("Mata kuliah sudah ditambahan !");
        }
        // proses
        if ($this->tnotification->run() !== FALSE) {
            // params
            $set_jur_1 = $this->input->post('set_jur_1');
            $set_jur_2 = $this->input->post('set_jur_2');
            $set_jur_3 = $this->input->post('set_jur_3');
            $set_jur_4 = $this->input->post('set_jur_4');
            //--
            $params = array(
                $set_makul_kd, $makul_kd,
                $set_jur_1, $set_jur_2, $set_jur_3, $set_jur_4,
                $this->com_user['user_id']
            );
            // insert 
            if ($this->m_makul->insert_detail_setting_makul($params)) {
                // sukses
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        //redirect
        redirect('pengaturan/makul/add_makul/' . $this->input->post('set_makul_kd'));
    }

    // halaman edit makul
    public function edit_makul($set_makul_kd = "", $makul_kd = "") {
        // set page rules
        $this->_set_page_rule("U");
        // set template content
        $this->smarty->assign("template_content", "pengaturan/makul/edit_makul.html");
        // load javascript
        $this->smarty->load_javascript("resource/js/jquery/select2-3.4.5/select2.min.js");
        // load css 
        $this->smarty->load_style("select2/select2.css");
        // detail grup
        $rs_group = $this->m_makul->get_detail_setting_makul($set_makul_kd);
        $this->smarty->assign('rs_group', $rs_group);
        // data makul
        $this->smarty->assign('rs_makul', $this->m_makul->get_all_data_makul());
        // data setting
        $result = $this->m_makul->get_detail_data_set_makul_detail(array($set_makul_kd, $makul_kd));
        $this->smarty->assign('result', $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses edit makul
    public function edit_makul_process() {
        // set page rules
        $this->_set_page_rule("U");
        // cek input
        $this->tnotification->set_rules('makul_kd', 'Mata Kuliah', 'required');
        $this->tnotification->set_rules('set_jur_1', 'D3TI');
        $this->tnotification->set_rules('set_jur_2', 'D3MI');
        $this->tnotification->set_rules('set_jur_3', 'S1TI');
        $this->tnotification->set_rules('set_jur_4', 'S1SI');
        $this->tnotification->set_rules('set_makul_kd', 'Kode Grup', 'required');
        $this->tnotification->set_rules('makul_kd_old', 'Mata Kuliah Old', 'required');
        // cek makul
        $makul_kd_old = $this->input->post('makul_kd_old');
        $makul_kd = $this->input->post('makul_kd');
        $set_makul_kd = $this->input->post('set_makul_kd');
        // proses
        if ($this->tnotification->run() !== FALSE) {
            // params
            $set_jur_1 = $this->input->post('set_jur_1');
            $set_jur_2 = $this->input->post('set_jur_2');
            $set_jur_3 = $this->input->post('set_jur_3');
            $set_jur_4 = $this->input->post('set_jur_4');
            //--
            $params = array(
                $set_makul_kd, $makul_kd,
                $set_jur_1, $set_jur_2, $set_jur_3, $set_jur_4,
                $this->com_user['user_id']
            );
            // delete 
            $this->m_makul->delete_detail_setting_makul(array($set_makul_kd, $makul_kd_old));
            // insert 
            if ($this->m_makul->insert_detail_setting_makul($params)) {
                // sukses
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
                // redirect 
                redirect('pengaturan/makul/edit_makul/' . $this->input->post('set_makul_kd') . '/' . $this->input->post('makul_kd'));
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        //redirect
        redirect('pengaturan/makul/edit_makul/' . $this->input->post('set_makul_kd') . '/' . $this->input->post('makul_kd_old'));
    }

    // halaman hapus makul
    public function delete_makul($set_makul_kd = "", $makul_kd = "") {
        // set page rules
        $this->_set_page_rule("D");
        // set template content
        $this->smarty->assign("template_content", "pengaturan/makul/delete_makul.html");
        // detail grup
        $rs_group = $this->m_makul->get_detail_setting_makul($set_makul_kd);
        $this->smarty->assign('rs_group', $rs_group);
        // data makul
        $this->smarty->assign('rs_makul', $this->m_makul->get_all_data_makul());
        // data setting
        $result = $this->m_makul->get_detail_data_set_makul_detail(array($set_makul_kd, $makul_kd));
        $this->smarty->assign('result', $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses hapus makul
    public function delete_makul_process() {
        // set page rules
        $this->_set_page_rule("D");
        // cek input
        $this->tnotification->set_rules('makul_kd', 'Mata Kuliah', 'required');
        $this->tnotification->set_rules('set_makul_kd', 'Kode Grup', 'required');
        // cek makul
        $makul_kd = $this->input->post('makul_kd');
        $set_makul_kd = $this->input->post('set_makul_kd');
        // proses
        if ($this->tnotification->run() !== FALSE) {
            // params
            $params = array(
                $set_makul_kd, $makul_kd
            );
            // delete 
            if ($this->m_makul->delete_detail_setting_makul($params)) {
                // sukses
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil dihapus");
                // redirect 
                redirect('pengaturan/makul/edit_group/' . $this->input->post('set_makul_kd'));
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal dihapus");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal dihapus");
        }
        //redirect
        redirect('pengaturan/makul/delete_makul/' . $this->input->post('set_makul_kd') . '/' . $this->input->post('makul_kd'));
    }

}